package com.itheima.health.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper,CheckItem> implements CheckItemService {

    @Override
    public List<CheckItem> findAll() {
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper();
        // 有效数据
        queryWrapper.eq("is_delete",0);
        List<CheckItem> list = list(queryWrapper);
        return list;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<CheckItem> page = null;
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete",0);
        if (StringUtils.isEmpty(queryPageBean.getQueryString())) {
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        } else {
            queryWrapper.and(
                    wrapper ->
                            wrapper.like("name", queryPageBean.getQueryString())
                                    .or()
                                    .like("code",queryPageBean.getQueryString())
            );
//            queryWrapper.like("name",queryPageBean.getQueryString());
//            queryWrapper.or();
//            queryWrapper.like("code",queryPageBean.getQueryString());

            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }
}
